{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
    <link rel="stylesheet" href="{% static 'sw/sweetalert.css' %}">
</head>
<body>

<h1>书籍展示</h1>


<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <a href="{% url 'addbook' %}" class="btn btn-primary">添加书籍</a>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>书名</th>
                    <th>出版日期</th>
                    <th>价格</th>
                    <th>出版社</th>
                    <th>作者</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                {% for book_obj in book_objs %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ book_obj.title }}</td>
                        <td>{{ book_obj.publishDate|date:'Y-m-d' }}</td>
                        <td>{{ book_obj.price }}</td>
                        <td>{{ book_obj.publishs.name }}</td>
                        <td>
{#                            {% for author_obj in book_obj.authors.all %}#}
{#                                {{ author_obj.name }}#}
{#                                {% if forloop.last %}#}
{##}
{#                                {% else %}#}
{#                                    ,#}
{#                                {% endif %}#}
{#                            {% endfor %}#}
                            {{ book_obj.get_authors_name }}

                        </td>
                        <td>
                            <a href="{% url 'editbook' book_obj.pk %}" class="btn btn-warning">编辑</a>
                            <a href="{% url 'delbook' %}?book_id={{ book_obj.pk }}" class="btn btn-danger">删除</a>
                            <button book_id="{{ book_obj.pk }}" class="btn btn-danger ajaxbtn" >ajax删除</button>

                        </td>



                    </tr>


                {% endfor %}

                </tbody>


            </table>

        </div>
    </div>

</div>


<script src="{% static 'js/jquery.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
<script src="{% static 'sw/sweetalert.min.js' %}"></script>

<script>

    $(".ajaxbtn").on("click", function () {
        var ths = $(this);

      swal({
        title: "你确定要删除吗？",
        text: "删除可就找不回来了哦！",
        type: "warning",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        closeOnConfirm: false
        },
        function () {
          //var deleteId = $(this).parent().parent().attr("data_id");
          //console.log(ths);
          //console.log($(this));
            var book_id = ths.attr('book_id');
          $.ajax({
            url: "/delbook/",
            type: "post",
            data: {"book_id": book_id,'csrfmiddlewaretoken':"{{ csrf_token }}"},
            success: function (data) {
              if (data.status === 1) {
                swal("删除成功!", "你可以准备跑路了！", "success");
                // location.reload();
                ths.parent().parent().remove();

              } else {
                swal("删除失败", "你可以再尝试一下！", "error")
              }
            }
          })
        });
    })

</script>

</body>
</html>